using System;
using System.Collections.Generic;

using log4net;

using GymManager.Exception;
using GymManager.Mechanism;
using GymManager.Persistence.DAO;
using GymManager.Persistence.VO;

namespace GymManager.Business
{
    /// <summary>
    /// Class responsible by providing services regarding modalitites
    /// </summary>
    public class ModalityServices
    {
        // logger instance
        private ILog _logManager = LogManager.GetLogger(typeof(ModalityServices));

        /// <summary>
        /// Method responsible by getting information about modality in according to a given description
        /// </summary>
        /// <param name="String">modality's description</param>
        /// <returns>Modality - user value object matching with given description</returns>
        /// <see cref="GymManager.Exception.BusinessException"/>
        public Modality findByDescription(String description)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }
            
            Modality modalityResult = null;

            try
            {
                // checking if the modality exists
                ModalityDAO modalityDAO = new ModalityDAO();
                modalityResult = modalityDAO.findByDescription(description);
            }
            catch (PersistenceException)
            {
                throw new BusinessException();
            }            
            
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return modalityResult;
        }

        /// <summary>
        /// Method responsible by finding all active modalities
        /// </summary>
        /// <returns>IList - list of all active modalities</returns>
        /// <see cref="GymManager.Exception.BusinessException"/>
        public IList<Modality> findAllActive()
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            IList<Modality> modalityList = null;

            try
            {
                // getting information about modalities
                ModalityDAO modalityDAO = new ModalityDAO();
                modalityList = modalityDAO.findAllActive();
            }
            catch (PersistenceException)
            {
                throw new BusinessException();
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return modalityList;
        }


        /// <summary>
        /// Method responsible by finding modalities using any of its properties
        /// </summary>
        /// <param name="Modality">modality vo</param>
        /// <returns>IList - list of modalities retrieved in according to filter</returns>
        /// <see cref="GymManager.Exception.BusinessException"/>
        public IList<Modality> findByAny(Modality modalityVOFilter)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            IList<Modality> modalityList = null;

            try
            {
                // formating information of modality VO filter
                if (!String.IsNullOrEmpty(modalityVOFilter.Description))
                {
                    modalityVOFilter.Description = "%" + modalityVOFilter.Description + "%";
                }

                // getting information about modalities
                ModalityDAO modalityDAO = new ModalityDAO();
                modalityList = modalityDAO.findByAny(modalityVOFilter);
            }
            catch (PersistenceException)
            {
                throw new BusinessException();
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return modalityList;
        }


        /// <summary>
        /// Method responsible by adding a new modality to database
        /// </summary>
        /// <param name="Modality">modality information to be saved</param>
        /// <see cref="GymManager.Exception.BusinessException"/>
        public void create(Modality modalityVO)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            try
            {
                // adding modality to database
                ModalityDAO modalityDAO = new ModalityDAO();
                modalityDAO.create(modalityVO);
            }
            catch (PersistenceException)
            {
                throw new BusinessException();
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }
        }

        /// <summary>
        /// Method responsible by updating a modality into database
        /// </summary>
        /// <param name="Modality">modality information to be saved</param>
        /// <see cref="GymManager.Exception.BusinessException"/>
        public void update(Modality modalityVO)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            try
            {
                // updating modality to database
                ModalityDAO modalityDAO = new ModalityDAO();
                modalityDAO.update(modalityVO);
            }
            catch (PersistenceException)
            {
                throw new BusinessException();
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }
        }
    }
}
